package uiLayer;
import controlLayer.*;
import modelLayer.*;
import java.util.Scanner;
import java.util.ArrayList;

/**
 * PersonUI is the window you meet when you enter "Friends" in the Main Menu.
 * This menu makes it possible for the user to create, find, update and delete
 * friends in the system. It is also possible to go back to the Main Menu.
 * 
 * @author (Gruppe E) 
 * @version (06-11-2014)
 */
public class PersonUI
{
    PersonController personController;

    /**
     * Constructor for objects of class PersonUI
     */
    public PersonUI(PersonController personController)
    {
        this.personController = personController;
    }

    /**
     * Starts the program.
     */
    public void start()
    {
        friendMenu();
    }

    /**
     * This method checks which return value is chosen in writeFriendsMenu.
     * Each choice results in different methods. If the user chooses 5,
     * the menu closes and the user is returned to the Main Menu.
     */
    private void friendMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            String choice = writeFriendMenu();
            if(choice.equals("1"))
            {
                createFriend();
            }

            else if(choice.equals("2"))
            {
                findFriend();
            }

            else if(choice.equals("3"))
            {
                updateFriend();
            }

            else if(choice.equals("4"))
            {
                deleteFriend();
            }

            else if(choice.equals("5"))
            {
                friendLoanList();
            }
            else if(choice.equals("6"))
            {
                exit = true;
            }
            else
            {
                System.out.println("\nInvalid types. Please try again.");
            }
        }
    }

    /**
     * The menu that allows the user to create, find, update and delete
     * friends in the system. It is also possible to go back to 
     * the Main Menu.
     *
     * @return An int of the 5 different choices. 
     */
    private String writeFriendMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        blankLines();
        System.out.println("*** FriendsMenu ***");
        System.out.println("1. Create Friend");
        System.out.println("2. Find Friend");
        System.out.println("3. Update Friend");
        System.out.println("4. Delete Friend");
        System.out.println("5. Previous loans");
        System.out.println("6. Back");
        System.out.println("\nPlease choose a number");

        String choice = keyboard.nextLine();
        return choice;
    }

    /**
     * The system asks for the name of the friend. The user writes this 
     * on the next line.
     *
     * @return The name that the user has written.
     */
    private String inputFriendName()
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("\nEnter the name of the friend: ");
        String name = scanner.nextLine();
        return name;
    }

    /**
     * The system asks for the address of the friend. The user writes this 
     * on the next line.
     *
     * @return The address that the user has written.
     */
    private String inputFriendAdress()
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("\nEnter the address: ");
        String address = scanner.nextLine();
        return address;
    }

    /**
     * The system asks for the zip code that the friend has. The user writes this 
     * on the next line.
     *
     * @return The zip code that the user has written.
     */
    private String inputFriendZip()
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("\nEnter the zip code: ");
        String zip = scanner.nextLine();
        return zip;
    }

    /**
     * The system asks for the city that the friend lives in. The user writes this 
     * on the next line.
     *
     * @return The city that the user has written.
     */
    private String inputFriendCity()
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("\nEnter the city: ");
        String city = scanner.nextLine();
        return city;
    }

    /**
     * The system asks for the phone number of the friend. The user writes this 
     * on the next line.
     *
     * @return The phone number that the user has written.
     */
    private String inputFriendPhone()
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("\nEnter phone number: ");
        String phone = scanner.nextLine();
        return phone;
    }

    /**
     * This method calls the createPerson method via the personController.
     * It uses the return values from the 5 input-methods as parameters.
     */
    private void createFriend()
    {
        blankLines();
        System.out.println("\nRemember: Only one friend per phone number.");
        String phone = inputFriendPhone();
        if(personController.validatePhoneNumber(phone).equals("confirmed"))
        {
            String name = inputFriendName();
            String address = inputFriendAdress();
            String zip = inputFriendZip();
            String city = inputFriendCity();
            personController.createPerson(name, address, zip, city, phone);
            System.out.println("\nFriend succesfully created.");
        }
        else if(personController.validatePhoneNumber(phone).equals("letters"))
        {
            System.out.println("\nPhone number can't contain letters.");
        }
        else if(personController.validatePhoneNumber(phone).equals("numberExists"))
        {
            System.out.println("\nThe phone number already exists in the system.");
        }
    }

    /**
     * Requests the personControllerto find a person object. It writes the information
     * about the friend if this is found. Else it writes a failed message.
     *
     */
    private void findFriend()
    {
        blankLines();
        System.out.println("\nThe phone number of the friend you wish to find is required.");
        String phone = inputFriendPhone();
        Person p = personController.findPerson(phone);
        if(p == null)
        {
            System.out.println("\nNo results, please try again");
        }

        else if(p.getPhone().equals(p.getPhone()))
        {                    
            System.out.println();
            System.out.println("Name: " +p.getName());
            System.out.println("Address: " +p.getAddress());
            System.out.println("zip code: " +p.getZip());
            System.out.println("City: " +p.getCity());
            System.out.println("Phone: " +p.getPhone());
        }               
    }

    /**
     * This method calls the updatePerson method via the personController.
     * It takes the return value from all the input-methods as parameters.
     */
    private void updateFriend()
    {
        blankLines();
        System.out.println("\nThe phone number of the friend you wish to update is required.");
        String oldPhone = inputFriendPhone();
        if(personController.findPerson(oldPhone) !=null)
        {
            System.out.println("\nEnter the new information to change friend information.");
            String name = inputFriendName();        
            String address = inputFriendAdress();
            String zip = inputFriendZip();
            String city = inputFriendCity();
            String phone = inputFriendPhone();
            personController.updatePerson(oldPhone, name, address, zip, city, phone);
            System.out.println("\nFriend succesfully updated.");
        }
        else
        {
            System.out.println("\nFriend not found. I'm sorry about that.");
        }
    }

    /**
     * This method calls the deletePerson method via the personController.
     * It takes the return value from the inputFriendPhone method as a parameter.
     */
    private void deleteFriend()
    {
        blankLines();
        System.out.println("\nThe phone number of the friend you wish to delete is required.");
        String phone = inputFriendPhone();
        if(personController.deletePerson(phone))
        {
            System.out.println("\nFriend succesfully deleted.");
        }
        else
        {
            System.out.println("\nCould not find friend to delete.");
        }
    }

    /**
     * Prints out all loans that a friend with the given number has made
     * since he/she was created in the system.
     */
    private void friendLoanList()
    {
        blankLines();
        System.out.println("\nTo retrieve a loan list from a friend, please enter their phone number.");
        String phone = inputFriendPhone();

        if(personController.validatePhoneNumber(phone).equals("letters"))
        {
            System.out.println("\nPhone number can't contain letters.");
        }
        else if(personController.findPerson(phone) == null || personController.getLoans(phone) == null)
        {
            System.out.println("\nFriend not found. Please try again.");
        }
        else
        {    
            System.out.println("\nLoan list for " +personController.findPerson(phone).getName()+":");
            for(int i = 0; i < personController.getLoans(phone).size(); i++)
            {
                System.out.println(personController.getLoans(phone).get(i).getCopy().getDvd().getTitle());
            }
        }
    }

    /**
     * This method adds 5 blank lines.
     */
    private void blankLines()
    {
        for(int i = 0; i < 5; i++)
        {
            System.out.println();
        }
    }
}
